package com.zhong.gang.app.ui.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zhong.gang.app.R;
import com.zhong.gang.app.db.UserUtils;
import com.zhong.gang.app.ui.audit.AuditListAct;
import com.zhong.gang.app.ui.examine.ExamineListAct;
import com.zhong.gang.app.ui.login.MineAct;
import com.zhong.gang.app.ui.message.MessageAct;
import com.zhong.gang.app.ui.waidiao.detail.WaiDiaoDetailAct;
import com.zhong.gang.app.ui.waidiao.input.WaiDiaoAct;
import com.zhong.gang.app.ui.zhongzhuan.fa.ZhongZhuanFaAct;
import com.zhong.gang.app.ui.zhongzhuan.fa.ZhongZhuanFaDetailAct;
import com.zhong.gang.app.ui.zhongzhuan.shou.ZhongZhuanShouAct;
import com.zhong.gang.app.ui.zhuchang.changsure.FaHuoChangAct;
import com.zhong.gang.app.ui.zhuchang.detail.ZhuChangDetailAct;
import com.zhong.gang.app.ui.zhuchang.input.ZhuChangAct;
import com.zhong.gang.applib.config.Config;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.http.callback.DataCallBack;
import com.zhong.gang.applib.pop.TypeBean;
import com.zhong.gang.applib.pop.TypePop;
import com.zhong.gang.applib.ui.view.LeftTitleView;
import com.zhong.gang.applib.ui.view.RightTitleView;
import com.zhong.gang.applib.utils.CommonTitleBar;
import com.zhong.gang.applib.utils.ScanUtils;

import java.util.ArrayList;
import java.util.List;

public class MainController
        implements PopupWindow.OnDismissListener, TypePop.onTypeItemClickedListener,
        BaseQuickAdapter.OnItemClickListener {
    private int type, status;
    private TextView typeTv, statusTv;
    private ImageView typeImg, statusImg;
    private TypePop typePop, statusPop;
    private MainListener listener;
    private LeftTitleView leftTitleView;
    private Activity activity;


    public MainController(Activity activity, MainListener listener) {
        this.listener = listener;
        this.activity = activity;
    }

    protected void getTypeList(List<TypeBean> data, String title, int type) {
        if (data == null)
            return;
        data.add(new TypeBean(title, type));
    }

    /**
     * 初始化分类
     *
     * @param typeView
     * @param typeTv
     * @param typeImg
     */
    public void initTypePop(View typeView, TextView typeTv, ImageView typeImg,
            TextView newOrderTv) {
        this.typeTv = typeTv;
        this.typeImg = typeImg;
        List<TypeBean> typeList = new ArrayList<>();
        int roleId = UserUtils.getIntent(activity).getRoleId();
        if (roleId == 7) {//中转站
            getTypeList(typeList, "全部", 0);
            getTypeList(typeList, "中转收货", 4);
            getTypeList(typeList, "中转发货", 5);
            type = 0;
            newOrderTv.setText("新建中转发货");
        } else if (roleId == 5) {//驻场人员
            getTypeList(typeList, "全部", 0);
            getTypeList(typeList, "驻厂接单", 2);
            getTypeList(typeList, "到厂确认", 3);
            type = 0;
            newOrderTv.setText("新增驻厂接单");
        } else if (roleId == 9) {//外调人员
            typeTv.setText("外调货业");
            getTypeList(typeList, "外调货业", 1);
            newOrderTv.setText("新增外调货业");
            type = 1;
        } else if (roleId == 12 || roleId == 13 || roleId == 14 || roleId == 15) {//管理员
            getTypeList(typeList, "全部", 0);
            getTypeList(typeList, "外调货业", 1);
            getTypeList(typeList, "驻厂接单", 2);
            getTypeList(typeList, "到厂确认", 3);
            getTypeList(typeList, "中转收货", 4);
            getTypeList(typeList, "中转发货", 5);
            type = 0;
        }
        typePop = new TypePop(activity, typeView, typeList, this);
        typePop.setOnDismissListener(this);
    }

    /**
     * 初始化状态
     *
     * @param statusView
     * @param statusTv
     * @param statusImg
     */
    public void initStatusPop(View statusView, TextView statusTv, ImageView statusImg) {
        this.statusTv = statusTv;
        this.statusImg = statusImg;
        List<TypeBean> statusList = new ArrayList<>();
        getTypeList(statusList, "全部", 0);
        getTypeList(statusList, "未处理", 1);
        getTypeList(statusList, "已处理", 2);
        statusPop = new TypePop(activity, statusView, statusList, this);

        statusPop.setOnDismissListener(this);
    }


    /**
     * 初始化身份
     */
    public void initIdentity(View examine, View scan, View newOrder) {
        int roleId = UserUtils.getIntent(activity).getRoleId();
        examine.setVisibility(
                (roleId == 12 || roleId == 13 || roleId == 14||roleId==15) ? View.VISIBLE : View.GONE);
        scan.setVisibility((roleId == 5 || roleId == 7) ? View.VISIBLE : View.GONE);
        scan.setVisibility(View.GONE);
        newOrder.setVisibility(
                (roleId == 5 || roleId == 7 || roleId == 9) ? View.VISIBLE : View.GONE);
    }


    public void showPop(ImageView imageView, View view, TypePop popupWindow, int status) {
        imageView.setImageResource(com.zhong.gang.applib.R.mipmap.icon_up);
        popupWindow.showAsDropDown(view, status);
    }

    public TypePop getTypePop() {
        return typePop;
    }

    public void setTypePop(TypePop typePop) {
        this.typePop = typePop;
    }

    public TypePop getStatusPop() {
        return statusPop;
    }

    public void setStatusPop(TypePop statusPop) {
        this.statusPop = statusPop;
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.new_order://新订单
                int roleId = UserUtils.getIntent(activity).getRoleId();
                if (roleId == 5)
                    activity.startActivity(new Intent(activity, ZhuChangAct.class));
                else if (roleId == 7)
                    activity.startActivity(new Intent(activity, ZhongZhuanFaAct.class));
                else if (roleId == 9)
                    activity.startActivity(new Intent(activity, WaiDiaoAct.class));
                break;
            case R.id.scan://扫一扫
                ScanUtils.openScan(activity, Config.SCAN);
                break;
            case R.id.examine://审核
                activity.startActivity(new Intent(activity, AuditListAct.class));
                break;
        }
    }

    /**
     * 初始化左视图
     */
    public void initLeftView(CommonTitleBar commonTitleBar) {
        leftTitleView = new LeftTitleView(activity);
        leftTitleView.setImgRes(R.mipmap.icon_message);
        leftTitleView.setContent(0);
        commonTitleBar.setLeftView(leftTitleView);
        leftTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.startActivity(new Intent(activity, MessageAct.class));
            }
        });
    }

    /**
     * 右边
     */
    public void initRightView(CommonTitleBar commonTitleBar) {
        RightTitleView rightTitleView = new RightTitleView(activity);
        rightTitleView.setImgRes(R.mipmap.icon_mine);
        commonTitleBar.setRightView(rightTitleView);
        rightTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.startActivity(new Intent(activity, MineAct.class));
            }
        });
    }

    /**
     * 获取未读信息
     *
     * @param context
     * @param presenter
     */
    public void getMsgUnReadCount(Context context, MainPresenter presenter) {
        presenter.getMsgUnReadCount(context, new DataCallBack<BaseDataBean<Integer>>() {
            @Override
            public void onResponse(BaseDataBean<Integer> data) throws Exception {
                super.onResponse(data);
                leftTitleView.setContent(data.getResult());
            }
        });
    }


    @Override
    public void onDismiss() {
        typeImg.setImageResource(R.mipmap.icon_down);
        statusImg.setImageResource(R.mipmap.icon_down);
    }

    @Override
    public void onTypeItem(View view, TypeBean typeBean) {
        if (typeBean == null)
            return;
        int i = view.getId();
        if (i == R.id.type_layout) {
            type = typeBean.getType();
            typeTv.setText(typeBean.getTitle());
        } else if (i == R.id.status_layout) {
            status = typeBean.getType();
            statusTv.setText(typeBean.getTitle());
        }
        if (listener != null)
            listener.onRefresh();
    }

    public int getType() {
        return type;
    }

    public int getStatus() {
        return status;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        MainBean mainBean = (MainBean) adapter.getItem(position);
        setType(mainBean.getType(), mainBean.getId());
    }

    public void setType(int type, int id) {
        if (type == 1) {//外调
            WaiDiaoDetailAct.openAct(activity, id);
        } else if (type == 2) {//驻场接单
            ZhuChangDetailAct.openAct(activity, id);
        } else if (type == 3) {//发货到场
            FaHuoChangAct.openAct(activity, id);
        } else if (type == 4) {//中转收货
            ZhongZhuanShouAct.openAct(activity, id);
        } else if (type == 5) {//中转发货
            ZhongZhuanFaDetailAct.openAct(activity, id);
        }
    }

    public interface MainListener {
        void onRefresh();
    }
}
